/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.CalendarDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CalendarTBL;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;

/**
 * 
 * @author Patrick kendzo Email: pkendzo@msn.com
 */

@Stateless
public class CalendarDAO implements CalendarDAOInt
{

    @PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em;

    public CalendarDAO()
    {
    }

    public void persist(CalendarTBL todo)
    {
	em.persist(todo);
	em.flush();
    }

    public void delete(Long id)
    {
	CalendarTBL cal = em.find(CalendarTBL.class, id);

	CustomerTBL cust = cal.getCustomer();

	cust.getAppointments().remove(cal);

	em.remove(cal);
	em.flush();
    }

    public void update(CalendarTBL todo)
    {
	em.merge(todo);
	em.flush();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CalendarTBL> findRVByCustomer(String customer)
    {
	try
	{

	    List<CalendarTBL> resultList = (List<CalendarTBL>) em.createQuery(
		    "select c from CalendarTBL c where c.customer = '"
			    + customer + "' order by c.datum desc")
		    .getResultList();
	    return resultList;
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "No Calendar entry found for the given Customer", e);
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CalendarTBL> listRVByDate(Calendar date)
    {

	try
	{
	    int tmp = date.get(Calendar.MONTH) + 1;

	    List<CalendarTBL> resultList = (List<CalendarTBL>) em.createQuery(
		    "select c from CalendarTBL c where c.datum = '"
			    + date.get(Calendar.YEAR) + "-" + tmp + "-"
			    + date.get(Calendar.DAY_OF_MONTH) + "'")
		    .getResultList();
	    return resultList;
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "No ENTRY for a DATE couldn't been found", e);
	}
    }

    @Override
    public List<CalendarTBL> findAvailableRVFor_A_Period()
    {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public CalendarTBL getEntryByDate_And_Time(Calendar date, String time)
    {
	/*
	 * try { int tmp = date.get(Calendar.MONTH) + 1;
	 * 
	 * return (CalendarTBL)
	 * em.createQuery("select c from CalendarTBL c where c.datum = '" +
	 * date.get(Calendar.YEAR) +"-"+ tmp
	 * +"-"+date.get(Calendar.DAY_OF_MONTH) +"'"+" and c.rv_uhrzeit = '"
	 * +time+"'").getSingleResult();
	 * 
	 * } catch (Exception e) { throw new
	 * RuntimeException("No Calendar entry found for the given Date and Time "
	 * , e); }
	 */
	try
	{
	    int tmp = date.get(Calendar.MONTH) + 1;
	    CalendarTBL result = (CalendarTBL) em.createQuery(
		    "select c from CalendarTBL c where c.datum = '"
			    + date.get(Calendar.YEAR) + "-" + tmp + "-"
			    + date.get(Calendar.DAY_OF_MONTH) + "'"
			    + " and c.rv_uhrzeit = '" + time + "'")
		    .getSingleResult();

	    if (result != null)
	    {
		return result;
	    }

	    else
	    {
		return null;
	    }

	}
	catch (Exception e)
	{	    
	    return null;
	    // throw new
	    // RuntimeException("No Calendar entry found for the given Date and Time ",
	    // e);
	}

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CalendarTBL> findRVByCustomer(String customer,
	    String fromDatum, String toDatum)
    {
	try
	{

	    List<CalendarTBL> resultList = (List<CalendarTBL>) em.createQuery(
		    "select c from CalendarTBL c where c.customer = '"
			    + customer + "' and (c.datum between "
			    + "STR_TO_DATE('" + fromDatum
			    + "','%d.%m.%Y') and  STR_TO_DATE('" + toDatum
			    + "','%d.%m.%Y'))  order by c.datum desc")
		    .getResultList();
	    return resultList;
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "No Calendar entry found for the given Customer in this Period",
		    e);
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<CalendarTBL> findRVBySearchString(String suchBezeichner)
    {
	try
	{

	    List<CalendarTBL> resultList = (List<CalendarTBL>) em.createQuery(
		    "select c from CalendarTBL c where c.untersuchungstyp like '"
			    + suchBezeichner + "' order by c.datum desc").getResultList();
	    return resultList;
	}
	catch (Exception e)
	{
	    throw new RuntimeException(
		    "No Calendar entry found for the given search string", e);
	}
    }

}
